package com.example.demo.api.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.example.demo.api.IEmailMicroService;
import com.example.demo.cmiemail.EmailHelper;
import com.example.demo.cmiemail.SystemUtil;
import org.apache.commons.mail.EmailException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;

import java.util.List;
import java.util.Map;

/**
 * Created by Hy on 2018/8/6.
 */
@Service(protocol = "dubbo")
@ComponentScan
public class EmailMicroService implements IEmailMicroService {
    private static final Logger log = LoggerFactory.getLogger(EmailMicroService.class);

    @Autowired
    private EmailHelper emailHelper;

    /**
     * 发送文本邮件
     * @param emailAddr 地址
     * @param title 标题
     * @param content 文本内容
     * @param type 邮件类型
     */
    @Override
    public void sendMsgEmail(String emailAddr, String title, String content, EMAIL_TYPE type) {
     log.info("sendMsgEmail param:{},{},{},{}",emailAddr,title,content,type);
     if("0".equals(SystemUtil.emailFlag)){
         try {
             if(type.status==EMAIL_TYPE.FORGOTPWD.status){
                 emailHelper.sendCodeToEmail(emailAddr,content);
             }else if(type.status==EMAIL_TYPE.REGISTER.status){
                 emailHelper.CrmSendMsgToEmail(emailAddr,title,content);
             }else if(type.status==EMAIL_TYPE.HTML.status){
                 emailHelper.SendHtmlEmail(emailAddr,title,content);
             }else if(type.status==EMAIL_TYPE.VOICESENDMSG.status){
                 emailHelper.VoiceSendMsgToEmail(emailAddr,title,content);
             }else{
                 log.info("send email channel to be closed..............");
             }

         } catch (EmailException e) {
             e.printStackTrace();
         }
     }
    }

    /**
     * 发送附件邮件
     * @param emailAddr 地址
     * @param title 标题
     * @param content 文本内容
     * @param attachments 附件
     *                    {attachPath:url 附件地址目前只支持url
     *                    attachName:attachName, 附件名称
     *                    attachDesc:attachDesc 附件描述
     * }
     * @param type 邮件类型
     */
    @Override
    public void sendAttachmentToEmail(String emailAddr, String title, String content, List<Map<String, String>> attachments, EMAIL_TYPE type) {
      if("0".equals(SystemUtil.emailFlag)){

      }
    }

    /**
     * 发送邮件
     * @param paramMap
     * @param type 邮件类型
     */
    @Override
    public void sendMsgToEmail(Map<String, String> paramMap, EMAIL_TYPE type) {

    }
}
